## written by Mroylib
from mroylib.servers.tornado import Base, Manifest,check,WebSocketHandler , SocketHandler
import json


class InfoHandler(SocketHandler):
    route = "info"
    def open(self):
        super(InfoHandler, self).open()
        text = """
/* Example */
digraph {
    /* Note: HTML labels do not work in IE, which lacks support for <foreignObject> tags. */
    node [rx=5 ry=5 labelStyle="font: 300 14px 'Helvetica Neue', Helvetica"]
    edge [lineInterpolate=basis]
    
    A [labelType="html"
       label="A <span style='font-size:32px'>Big</span> <span style='color:red;'>HTML</span> Source!"];
    C;
    E [label="Bold Red Sink" style="fill: #f77; font-weight: bold"];
    A -> B [style="stroke: blue; stroke-width: 2px;" lineInterpolate=basis arrowheadStyle="fill: blue" ];
    B -> C;
    B -> D [label="A blue label" labelStyle="fill: #55f; font-weight: bold;"                 lineInterpolate=basis];
    D -> E [label="A thick red edge" style="dashed" arrowheadStyle="fill: #f77"];
    C -> E;
    A -> D [labelType="html" label="A multi-rank <span style='color:blue;'>HTML</span> edge!"
                style="stroke: blue; stroke-width: 2px;"
                lineInterpolate=basis
                ];
   B -> E [lineInterpolate=basis]
}

        """

        self.json_reply({
            "type": "sys",
            "code": "true",
            "text": text
        })


    def on_close(self):
        super(InfoHandler, self).on_close()


    def on_message(self, msg):
        # this is base function to write msg
        # self.write_message(msg)

        # this is reply msg to all connection
        # SocketHandler.send_to_all(msg)

        # this is json ver to reply msg
        self.json_reply(msg)


class Index(Base):
    @check
    def get(self):
        return ["Hello"]

    @check
    def post(self, graph):
        InfoHandler.send_to_all({
            "type": "sys",
            "code": "true",
            "text": graph
        
        })
        return "i got" + graph



class Socket(Base):
    @check
    def get(self):
        return "Hello"

    @check
    def post(self, name):
        return "i got" + name



class Gui(Base):
    @check
    def get(self):
        return ["Hello"]

    @check
    def post(self, name):
        return "i got" + name
